import { isObject, forEach, isNull, isUndefined } from 'lodash'

import axios from 'axios'
import { MessageBox } from 'element-ui-for-gov'
const doRequest = function (reqUrl, reqData = {}, reqMethod = 'POST', reqType = 'json', showError = true, contextPath = '') {
    return new Promise(function(resolve, reject) {
        // 请求头部
        const headers = {}
        let responseType = ''
        if (reqUrl !== '/calendar/queryWorkDateAfterDaysOne' && reqData && ![ 'multipart', ].includes(reqType)) {
            reqData.opTerminalType = 'PC'
        }
        // 请求参数
        let params
        if (reqType === 'json') {
            headers['Content-Type'] = 'application/json;charset=UTF-8'
            params = JSON.stringify(reqData)
        } else if (reqType === 'file_json') {
            headers['Content-Type'] = 'application/json;charset=UTF-8'
            params = JSON.stringify(reqData)
            responseType = 'blob'
        } else if (reqType === 'file') {
            responseType = 'blob'
            headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
            params = createParams(reqData)
        } else if (reqType === 'multipart') {
            headers['Content-Type'] = 'multipart/form-data;'
            params = reqData
        } else {
            headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
            params = createParams(reqData)
        }
        axios({
            method: reqMethod.toLowerCase(),
            url: (contextPath || '') + reqUrl,
            data: reqMethod.toLowerCase() !== 'get' ? params : null,
            params: reqMethod.toLowerCase() === 'get' ? params : null,
            headers: headers,
            responseType: responseType,
        }).then(function (response) {
            if (responseType === 'blob') {
                resolve(response)
            } else {
                resolve(response.data)
            }
        }).catch(function (error) {
            if (showError) {
                let status
                let showMsg
                if (error.response) {
                    status = error.response.status
                } else if (error.request) {
                    status = 502
                } else {
                    status = 501
                }

                if (status === 401) {
                    showMsg = '未登陆'
                } else if (status === 403) {
                    showMsg = '您没有访问权限'
                } else if (status === 404) {
                    showMsg = '请求不存在'
                } else if (status === 400) {
                    showMsg = '服务器无法解析参数'
                } else if (status === 500) {
                    showMsg = '服务器异常'
                } else if (status === 502) {
                    showMsg = '服务不可用'
                } else {
                    showMsg = '无效的请求'
                }
                if (process.env.NODE_ENV === 'production') {
                    setTimeout(() => {
                        MessageBox.alert(reqUrl + showMsg).catch(() => {})
                    })
                }
            }
            reject({
                response: error,
            })
        })
    })
}

const ajaxRequest = {

    doRequest,

}

export const toUrlParams = (obj) => {
    return createParams(obj).toString()
}

const createParams = (obj) => {
    const params = new URLSearchParams()
    if (isObject(obj)) {
        forEach(obj, function (val, key) {
            if (!isUndefined(val) && !isNull(val)) {
                params.append(key, val)
            }
        })
    }
    return params
}

export default ajaxRequest
